package au.edu.swin.videoshop.persistence;

import java.io.Serializable;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@NamedQueries({
	@NamedQuery (
			name="findAllTitles", 
			query="SELECT t FROM TitleEntity t WHERE t.deleted = FALSE"
	),
	@NamedQuery (
			name="findTitlesByCategoryId", 
			query="SELECT t FROM TitleEntity t WHERE t.deleted = FALSE AND t.category.titleCategoryId = :titleCategoryId"
	),
	@NamedQuery (
			name="findTitlesInAnyCategory", 
			query="SELECT t FROM TitleEntity t WHERE " +
					"t.deleted = FALSE AND " +
					"LOWER(t.titleName) LIKE :titleName AND " +
					"t.classification LIKE :classification"
	),
	@NamedQuery (
			name="findTitles", 
			query="SELECT t FROM TitleEntity t WHERE " +
					"t.deleted = FALSE AND " +
					"LOWER(t.titleName) LIKE :titleName AND " +
					"t.category.titleCategoryId = :titleCategoryId AND " +
					"t.classification LIKE :classification"
	)
})
@Table(name="TITLE")
public class TitleEntity implements Serializable{
	private Long mTitleId;
	private String mTitleName;
	private String mDescription;
	private TitleCategoryEntity mCategory;
	private String mClassification;
	private int mYear;
	private List mMedia;
	
	//Attribute to mark the entity as deleted
	private boolean mDeleted;
	private String mDeleteInfo;
	
	public TitleEntity() {
		mDeleted = false;
	}
	
	/**
	 * @return the titleId
	 */
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name="TITLE_ID")
	public Long getTitleId() {
		return mTitleId;
	}

	/**
	 * @param titleId the titleId to set
	 */
	public void setTitleId(Long titleId) {
		mTitleId = titleId;
	}
	
	/**
	 * @return the titleName
	 */
	@Column(name="TITLE_NAME", length=50)
	public String getTitleName() {
		return mTitleName;
	}
	
	/**
	 * @param titleName the titleName to set
	 */
	public void setTitleName(String titleName) {
		mTitleName = titleName;
	}
	
	/**
	 * @return the description
	 */
	@Column(name="TITLE_DESC", length=500)
	public String getDescription() {
		return mDescription;
	}
	
	/**
	 * @param description the description to set
	 */
	public void setDescription(String description) {
		mDescription = description;
	}
	
	/**
	 * @return the category
	 */
	@ManyToOne(optional=false, fetch=FetchType.EAGER)
	@JoinColumn(name="TITLE_CATEGORY_ID")
	public TitleCategoryEntity getCategory() {
		return mCategory;
	}
	
	/**
	 * @param category the category to set
	 */
	public void setCategory(TitleCategoryEntity category) {
		mCategory = category;
	}
	
	/**
	 * @return the classification
	 */
	@Column(name="CLASSIFICATION")
	public String getClassification() {
		return mClassification;
	}

	/**
	 * @param classification the classification to set
	 */
	public void setClassification(String classification) {
		mClassification = classification;
	}

	/**
	 * @return the year
	 */
	@Column(name="RELEASE_YEAR")
	public int getYear() {
		return mYear;
	}

	/**
	 * @param year the year to set
	 */
	public void setYear(int year) {
		mYear = year;
	}

	/**
	 * @return the media
	 */
	@OneToMany(mappedBy="title", cascade=CascadeType.ALL, targetEntity=TitleMediumEntity.class, fetch=FetchType.EAGER)
	public List getMedia() {
		return mMedia;
	}

	/**
	 * @param media the media to set
	 */
	public void setMedia(List media) {
		mMedia = media;
	}
	
	/**
	 * @return the deleted
	 */
	@Column(name="IS_DELETED")
	public boolean isDeleted() {
		return mDeleted;
	}
	
	/**
	 * @param deleted the deleted to set
	 */
	public void setDeleted(boolean deleted) {
		mDeleted = deleted;
	}
	
	/**
	 * @return the deleteInfo
	 */
	@Column(name="DELETE_INFO")
	public String getDeleteInfo() {
		return mDeleteInfo;
	}
	
	/**
	 * @param deleteInfo the deleteInfo to set
	 */
	public void setDeleteInfo(String deleteInfo) {
		mDeleteInfo = deleteInfo;
	}
}
